package com.avengers.core.bean;


import com.avengers.core.bean.enums.SymbolEnum;
import com.avengers.core.kit.EncryptKit;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.concurrent.locks.ReentrantLock;

import static com.avengers.core.bean.Numbers.*;

/**
 * @author yu.wang
 * @since 2025/6/27 09:54
 **/
public class AvengersSerialLock extends ReentrantLock {
    private final String serialNo;

    public AvengersSerialLock() {
        this(null);
    }

    public AvengersSerialLock(String serialNo) {
        super();
        this.serialNo = null == serialNo || SymbolEnum.empty.string.equals(serialNo) ? genSerialNo() : serialNo;
    }

    public String getSerialNo() {
        return serialNo;
    }

    private static String genSerialNo() {
        String string = System.currentTimeMillis() + RandomStringUtils.randomNumeric(INT_10) + Thread.currentThread().getId();
        return "Lock_" + EncryptKit.md5(string).toLowerCase().substring(INT_0, INT_8);
    }
}
